package edu.tgc.web.controller.gard;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.abyss.common.web.controller.easyui.CommonCrudEasyUiController;
import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.tgc.entity.gard.Door;
import edu.tgc.service.gard.DoorService;
import edu.tgc.web.form.gard.DoorForm;
@Controller
@RequestMapping(value="/security/gard")
public class DoorController extends CommonCrudEasyUiController<Door, UUID, DoorForm> {
	
	@Autowired
	private DoorService service;
	
	@RequestMapping(value="/login")
	@ResponseBody
	public Object login(String idNo,Boolean auto,HttpServletRequest request,HttpServletResponse response) throws IOException{
		HashMap<String, Object> result=service.login(idNo,auto,request,response);
		return result;
	}
	
	@RequestMapping(value="/play")
	public String play(){
		return "security/gard/play";
	}

	@Override
	public Specification<Door> buildSearch(DoorForm form) {
		HashSet<Rule> rules=new HashSet<>();
		String name=form.getName();
		if(StringUtils.hasText(name))
			rules.add(Rule.property("name").contain(name));
		String idNo=form.getIdNo();
		if(StringUtils.hasText(idNo)){
			rules.add(Rule.property("idNo").contain(idNo));
		}
		String sex=form.getSex();
		if(StringUtils.hasText(sex)){
			rules.add(Rule.property("sex").equal(sex));
		}
		String nation = form.getNation();
		if(StringUtils.hasText(nation)){
			rules.add(Rule.property("nation").contain(nation));
		}
		String dept = form.getDept();
		if(StringUtils.hasText(dept)){
			rules.add(Rule.property("dept").contain(dept));
		}
		String status = form.getStatus();
		if(StringUtils.hasText(status)){
			rules.add(Rule.property("status").equal(status));
		}
		Date createTime_begin = form.getCreateTime_begin();
		if(createTime_begin!=null){
			rules.add(Rule.property("createTime").greaterThanOrEqualTo(createTime_begin));
		}
		Date createTime_end = form.getCreateTime_end();
		if(createTime_end!=null){
			rules.add(Rule.property("createTime").lessThanOrEqualTo(createTime_end));
		}
		return Rule.and(rules).toSpecification();
	}
	
	
}
